package cn.com.gridinfo.sooc.utils.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;
import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.classes.adapter.CourseListAdapter;
import cn.com.gridinfo.sooc.utils.widget.widget.adapters.AbstractWheelTextAdapter;
import cn.com.gridinfo.sooc.utils.widget.widget.views.OnWheelChangedListener;
import cn.com.gridinfo.sooc.utils.widget.widget.views.OnWheelScrollListener;
import cn.com.gridinfo.sooc.utils.widget.widget.views.WheelView;

/**
 * 滚轮效果的Dialog
 * Created by yingchunli on 2016/1/19.
 */
public class WheelDialog extends Dialog implements android.view.View.OnClickListener {

    private Context context;
    private WheelView wv;

    private int maxTextSize = 16;//最大字体
    private int minTextSize = 16;//最小字体
    private TextView btnSure;//确定按钮
    private TextView btnCancel;//取消按钮
    String currentText;//当前选中文字
    CourseListAdapter adapter;//适配器
    private List<String> list = new ArrayList<>();
    OnSelectCourseListener mOnSelectCourseListener;
    int currentPosition;//当前显示的item
    int selectPosition;//选择的item
    String title;//标题
    boolean cancel = true;//取消按钮是否显示，默认显示

    /**
     * 构造函数
     *
     * @param context
     * @param list    要显示的list数据
     * @param title   标题
     */
    public WheelDialog(Context context, List<String> list, String title) {
        super(context, R.style.ShareDialog);
        this.context = context;
        this.list = list;
        this.title = title;
    }

    /**
     * 构造函数
     *
     * @param context
     * @param list    list数据
     * @param title   标题
     * @param cancel  是否显示取消按钮  默认时true,  true:显示  false:不显示
     */
    public WheelDialog(Context context, List<String> list, String title, boolean cancel) {
        super(context, R.style.ShareDialog);
        this.context = context;
        this.list = list;
        this.title = title;
        this.cancel = cancel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_courselist_dialog);
        //获取标题组件
        TextView titleText = (TextView) findViewById(R.id.tv_share_title);
        titleText.setText(title);//设置标题
        wv = (WheelView) findViewById(R.id.list_wheel);
        adapter = new CourseListAdapter(context, list, 0, maxTextSize, minTextSize);
        btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
        btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel);
        //设置取消按钮的显示与否
        if (cancel) {
            btnCancel.setVisibility(View.VISIBLE);//显示取消按钮
        } else {
            btnCancel.setVisibility(View.GONE);//不显示取消按钮
        }
        btnSure.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        currentText = list.get(0);
        wv.setVisibleItems(3);
        wv.setViewAdapter(adapter);
        wv.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                currentText = (String) adapter.getItemText(wheel.getCurrentItem());
                selectPosition = wheel.getCurrentItem();
                setTextviewSize(currentText, adapter);
                currentPosition = wheel.getCurrentItem();
            }
        });

        wv.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                currentText = (String) adapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, adapter);
            }
        });
        setCanceledOnTouchOutside(true);
    }

    /**
     * 接口  用于选择课程
     */
    public interface OnSelectCourseListener {
        void onClick(int position);
    }

    public void setmOnSelectCourseListener(OnSelectCourseListener mOnSelectCourseListener) {
        this.mOnSelectCourseListener = mOnSelectCourseListener;
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (v == btnSure) {
            if (mOnSelectCourseListener != null) {
                mOnSelectCourseListener.onClick(selectPosition);
            }
        }
        dismiss();
    }

    /**
     * 设置当前的显示的内容
     */
    public void setCurrentItem(int position) {
        selectPosition = position;
        currentPosition = position;
    }

    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, CourseListAdapter adapter) {
        List<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            TextView textview = (TextView) arrayList.get(i);
            currentText = textview.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textview.setTextSize(maxTextSize);
                textview.setTextColor(context.getResources().getColor(R.color.color_tv333));
            } else {
                textview.setTextSize(minTextSize);
                textview.setTextColor(context.getResources().getColor(R.color.color_999));
            }
        }
    }

    /**
     * 适配器
     */
    private class CourseListAdapter extends AbstractWheelTextAdapter {
        List<String> list;

        protected CourseListAdapter(Context context, List<String> list, int currentItem, int maxsize, int minsize) {
            super(context, R.layout.expand_callname_courselist_item, NO_RESOURCE, currentItem, maxsize, minsize);
            this.list = list;
            setItemTextResource(R.id.tempValue);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            TextView textView = (TextView)view.findViewById(R.id.tempValue);
            String textStr  = textView.getText().toString();
            if (textStr.equals(currentText)) {
                textView.setTextSize(16);
                textView.setTextColor(context.getResources().getColor(R.color.color_tv333));
            } else {
                textView.setTextSize(14);
                textView.setTextColor(context.getResources().getColor(R.color.color_999));
            }
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index) + "";
        }
    }

}
